package com.heima.wemedia.listen;

import com.heima.common.constants.message.NewsAutoScanConstants;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @作者 itcast
 * @创建日期 2021/9/25 15:51
 **/
@Component
@Slf4j
public class WemediaNewsAutoListener {

    @Autowired
    WmNewsAutoScanService wmNewsAutoScanService;

    @RabbitListener(queuesToDeclare = {@Queue(name = NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_QUEUE)})
    public void autoScanHandler(@Payload String newsId, Channel channel, Message message){
        log.info(" 收到 文章自动审核消息    消息内容: {}",newsId);
        wmNewsAutoScanService.autoScanWmNews(Integer.valueOf(newsId));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);// 手动确认消费消息
        } catch (IOException e) {
            e.printStackTrace();
            log.error("消费者  确认消费消息失败  ========== 文章id: {} 错误原因: {}",newsId,e.getMessage());
        }
    }
}
